package com.itheima.reggie.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.Service.CategoryService;
import com.itheima.reggie.Service.DishFlavorService;
import com.itheima.reggie.Service.DishService;
import com.itheima.reggie.Service.EmployeeService;
import com.itheima.reggie.Sharing.DishOrDishDlavor;
import com.itheima.reggie.exception.R;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @PostMapping
    public R<String> addDishh(@RequestBody DishOrDishDlavor dishDlavor)
    {
        dishService.addDish(dishDlavor);
        return R.success("菜品添加成功");
    }
    //分页
    @GetMapping("/page")
   public R<Page> page(int page, int pageSize, String name)
    {
        Page<Dish> pageInfo=new Page(page,pageSize);

        Page<DishOrDishDlavor> dishDla=new Page<>(page,pageSize);
        QueryWrapper<Dish> dishQueryWrapper=new QueryWrapper<>();
        dishQueryWrapper.orderByDesc("sort");
        if(name!=null) {
            dishQueryWrapper.like("name",name);
        }
        dishService.page(pageInfo,dishQueryWrapper);
        //以下步骤是分页时菜品分类是空的解决方案
        //对象拷贝
        BeanUtils.copyProperties(pageInfo,dishDla,"records");
        List<Dish> records =pageInfo.getRecords();//获得分页数据

        List<DishOrDishDlavor> list=new ArrayList<>();
        for(Dish o :records)
        {
            DishOrDishDlavor dishDlavor=new DishOrDishDlavor();
            BeanUtils.copyProperties(o,dishDlavor);
             Long loId= o.getCategoryId();
            Category category =categoryService.getById(loId);
            String categoryName=category.getName();
            dishDlavor.setCategoryName(categoryName);
            list.add(dishDlavor);

        }

         dishDla.setRecords(list);


        return R.success(dishDla);
    }
    @PostMapping("/status/{status}")
    public R<String> qishou(Integer ids,@PathVariable Integer status)
    {
        QueryWrapper<Dish> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",ids);
        dishService.update(queryWrapper);
        return R.success("修改成功");
    }
//回显
@GetMapping("/{id}")
    public R<DishOrDishDlavor> update(@PathVariable Long id)
{
 DishOrDishDlavor dishDlavor=  dishService.update(id);

return R.success(dishDlavor);
}

//修改
@PutMapping
    public  R<String>  save(@RequestBody DishOrDishDlavor dishDlavor)
{

    System.out.println("11111");
    dishService.save(dishDlavor);
    return R.success("修改成功");
}
//做套餐列表的时候,通过菜品分类来添加菜品的需求
  /*  @GetMapping("/list")
    public R<List<Dish>> add(Dish dish)
    {

        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("category_id",dish.getCategoryId());
        queryWrapper.orderByDesc("sort");
       List<Dish> dishes= dishService.list(queryWrapper);
      return R.success(dishes);
    }*/


  @GetMapping("/list")
    public R<List<DishOrDishDlavor>> list(Dish dish)
  {
      //通过id查菜品
      QueryWrapper<Dish> queryWrapper=new QueryWrapper<>();
      queryWrapper.eq("category_id",dish.getCategoryId());
      queryWrapper.eq("status",1);
      queryWrapper.orderByAsc("sort");
      List<Dish> list=dishService.list(queryWrapper);
     List<DishOrDishDlavor> list22=new ArrayList<>();
      QueryWrapper<DishFlavor> queryWrapper1=new QueryWrapper<>();
      for(Dish dish1:list)
      {
          DishOrDishDlavor dishDlavor=new DishOrDishDlavor();
          BeanUtils.copyProperties(dish1,dishDlavor);
          queryWrapper1.eq("dish_id",dish1.getId());
         List<DishFlavor> flavors=dishFlavorService.list(queryWrapper1);
          dishDlavor.setFlavors(flavors);
          list22.add(dishDlavor);
      }

 return R.success(list22);


  }

}
